package com.example.japinhands.data

import android.content.Context.MODE_PRIVATE
import android.util.Log
import androidx.core.content.edit
import com.example.japinhands.JApplication

object WordConstant {
    const val NONE = -1
    const val NOUN = 0
    const val VERB_1 = 1
    const val VERB_2 = 2
    const val VERB_3 = 3
    const val ADJECTIVE_1 = 4
    const val ADJECTIVE_2 = 5
    const val ADVERB = 6
    const val CONJUNCTION = 7
    const val INTERJECTION = 8

    const val DEFORM_NULL = ""
    const val SENTENCE_KEY_NOT_EXIST = Long.MIN_VALUE

    private val sp = JApplication.context.getSharedPreferences("jap_sp", MODE_PRIVATE)
    private const val TAG = "WordConstant"
    private const val JAP_SENTENCE_KEY = "jap_sentence_key"


    /**
     * Sentence key starts from 1, thus save 0 when initializing.
     */
    val newSentenceKey: Long
        get() {
            var ret = sp.getLong(JAP_SENTENCE_KEY, -1)
            Log.d(TAG, "Old sentence key: $ret")
            return if (ret != -1L) {
                ++ret
                Log.d(TAG, "New sentence key: $ret")
                saveNewKey(ret)
                ret
            } else {
                Log.e(TAG, "Sentence key not exist: $ret")
                SENTENCE_KEY_NOT_EXIST
            }
        }

    val currSentenceKey: Long
        get() {
            var ret = sp.getLong(JAP_SENTENCE_KEY, SENTENCE_KEY_NOT_EXIST)
            Log.d(TAG, "Old sentence key: $ret")
            return if (ret != SENTENCE_KEY_NOT_EXIST) {
                ++ret
                Log.d(TAG, "New sentence key: $ret")
                ret
            } else {
                Log.e(TAG, "Sentence key not exist: $ret")
                ret
            }
        }

    fun saveNewKey(key: Long) = sp.edit {
        putLong(JAP_SENTENCE_KEY, key)
    }

    fun getWordType(type: Int): String = when (type) {
        NOUN -> "名词"
        VERB_1 -> "动1"
        VERB_2 -> "动2"
        VERB_3 -> "动3"
        ADJECTIVE_1 -> "形1"
        ADJECTIVE_2 -> "形2"
        ADVERB -> "副词"
        CONJUNCTION -> "连词"
        INTERJECTION -> "感叹词"
        else -> "错误"
    }

}
